package 几个重要的接口;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-07-01
 * Time: 17:13
 */
class Student implements  Comparable<Student> {//自定义比较类实现Comparable<自定义类>接口
    //实例成员
    public String name;
    public int age;
    public double score;
//构造方法
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
//重写对象打印
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    //实现Comparable<自定义类>接口 必须重写compareTo这个方法
    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;
        if (this.age > o.age) {
            return 1;
        } else if (this.age == o.age) {
            return 0;
        } else {
            return -1;
        }
////        if (o.age > this.age){
////            return 1;
////        }else if(this.age == o.age){
////            return 0;
////        }else{
////            return -1;
////        }
////    }
    }
}
class ScoreComparetor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score-o2.score);
    }
}
public class Test {

    public static void main1(String[] args) {
        Student[] student = new Student[3];
        //每一个数组元素都是一个学生对象
        student[0] = new Student("小王",19,85.0);
        student[1] = new Student("小李",17,75.0);
        student[2] = new Student("小刘",18,95.0);
        //实例化出比较器的对象
        ScoreComparetor scoreComparetor = new ScoreComparetor();
        System.out.println("排序前"+Arrays.toString(student));
        //自定义对象的引用,比较器的引用
        Arrays.sort(student,scoreComparetor);
        System.out.println("排序后"+Arrays.toString(student));
//        System.out.println("排序前"+Arrays.toString(student));
//        Arrays.sort(student);
//        System.out.println("排序后"+Arrays.toString(student));


    }




    public static void main(String[] args) {
        Student student = new Student("小王",19,85.0);
        Student student1 = new Student("小李",18,90.0);
//        //student引用调用重写的compareTo方法，student1引用作为参数
//        if(student.compareTo(student1)>0){
//            System.out.println("student>student1");
//        }else if(student.compareTo(student1)<0){
//            System.out.println("student>student1");
//        }else{
//            System.out.println("student=student1");
//        }
        ScoreComparetor scoreComparetor = new ScoreComparetor();
        int ret = scoreComparetor.compare(student,student1);
        System.out.println(ret);



  }
}
